<?php
/**
 * User: 未央-请保留版权
 * Agreement: 禁止使用本软件（系统）用于任何违法违规业务或项目,造成的任何法律后果允由使用者（或运营者）承担
 * Date: 2021/3/3
 * Time: 14:34
 */
namespace app\common\controller;
use think\Db;
use think\Controller;

class bibi_dc extends Controller
{
    public function getReturn($code = 1, $data = "成功", $json = null)
    {
        return array("code" => $code, "data" => $data, "json" => $json);
    }
    
    public function cehsald($atad)//监控回调
    {
        $sz = Db::name("pay_sz")->find();
        $row = Db::name("pay_gfg")->where("pid", $atad['pid'])->where("mid",$atad['mid'])->find();
        $header = array(
            'User-Agent:Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36',
          );  
        $ret = curl_get($atad['mid_json'],$header); 
        $json = json_decode($ret,TRUE);
        if($json['data']['status']=="4"){
           return $this->getReturn(1, '交易成功', $atad); 
        }else{
           return $this->getReturn(-1, '交易失败', $atad); 
        }
    }
    
    public function fukyem($data)//支付页面
    {
        $sz = Db::name("pay_sz")->find();
        $atad= Db::name("pay_gfg")->where("pid", $data['pid'])->where("mid", $data['mid'])->find();
        $user = Db::name("pay_user")->where("id", $data['pid'])->find();
        $data['timeOut'] = $user['bug_close'];//订单有效期
        
        if($data['type']=='alipay'){//支付宝
            if($atad['ms']==0){//H5
                $data['mid_url'] = "https://render.alipay.com/p/s/i?scheme=".urlencode("alipays://platformapi/startapp?saId=10000007&qrcode=".urlencode($data['mid_url']));//H5转跳链接
                // 模板变量赋值
                $this->assign('data',$data);
                return $this->fetch('pay/h5');
            }else{//扫码
                $data['src'] = url('pay/enQrcode').'?url='.$data['mid_url'];//二维码
                $data['mid_url'] = "https://render.alipay.com/p/s/i?scheme=".urlencode("alipays://platformapi/startapp?saId=10000007&qrcode=".urlencode($data['mid_url']));//H5转跳链接
                // 模板变量赋值
                $data['luji'] = '../../static/pay/pay_'.$sz['web_pay'];//模板路径
                $this->assign('data',$data);
                return $this->fetch('pay/pay_'.$sz['web_pay'].'/index');
            }
            
        }else{//微信
        
            if($atad['ms']==0){//H5
            
            }else{//扫码
                $data['src'] = url('pay/enQrcode').'?url='.$data['mid_url'];//二维码
                // 模板变量赋值
                $data['luji'] = '../../static/pay/pay_'.$sz['web_pay'];//模板路径
                $this->assign('data',$data);
                return $this->fetch('pay/pay_'.$sz['web_pay'].'/index');
            }
        }
    }
    
    public function exec($atad,$money,$type)
    {
        $sz = Db::name("pay_sz")->find();
        if($type=="weixin"){//微信
          if($atad['ms']==0){//H5
                return $this->getReturn(-1, $atad['typec_name']."该通道微信H5模式已关闭，等待更新中...");
          }
        }
        $header = [
                'content-type:application/json',
            ];
        $post_data = '{"platformType":3,"bp":"'.$money.'","feeType":"CNY","productId":"pc0bcoin","createUa":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36","returnUrl":"https://pay.bilibili.com/pay-v2-web/bcoin_index","productUrl":"https://pay.bilibili.com/pay-v2-web/bcoin_index","failUrl":"https://pay.bilibili.com/pay-v2-web/bcoin_index","deviceInfo":"pc","version":"2.0","deviceType":7}';
        $ret = curls('https://pay.bilibili.com/paywallet/recharge/requestRecharge',$post_data,$atad['ck'],$header);
        $json = json_decode($ret,TRUE);
        if($json['errno'] != 0){
            return $this->getReturn(-1, $atad['typec_name']."订单创建失败");
        }
        //支付地址
        $payurl =  $json['data']['code']['codeUrl']; 
        $orderId = $json['data']['payParam']['orderId'];
        //取出相关订单
        $traceId = $json['data']['queryOrderParam']['traceId']; 
        $customerId = $json['data']['queryOrderParam']['customerId']; 
        $sign = $json['data']['queryOrderParam']['sign']; 
        $mid = $json['data']['queryOrderParam']['mid']; 
        $wxId = $json['data']['queryOrderParam']['wxId']; 
        $timestamp = $json['data']['queryOrderParam']['timestamp']; 
        $check_url = 'https://pay.bilibili.com/paywallet/recharge/queryOrderV2?traceId='.$traceId.'&customerId='.$customerId.'&sign='.$sign.'&mid='.$mid.'&wxId='.$wxId.'&timestamp='.$timestamp;
        if($payurl == ''){
            return $this->getReturn(-1, $atad['typec_name']."支付地址获取失败");
        }
        $atad['pay_id'] = $orderId;//通道订单号
        $atad['mid_url'] = $payurl;//通道二维码
        return $this->getReturn(1, $atad, $check_url);
    }
}